package dao

import (
	"activity_srv/basic/config"
	__ "activity_srv/basic/proto"
	"activity_srv/handler/models"
	"fmt"
	"log"
	"math/rand"

	"errors"

	"time"
)

// 砍价活动查询
func GetCutActivity(activityId int) (models.CutActivity, error) {
	// 活动查询
	var a models.CutActivity
	a.Id = uint32(activityId)
	if err := a.GetById(); err != nil {
		return models.CutActivity{}, errors.New("查询活动失败" + err.Error())
	}
	if a.Title == "" {
		return models.CutActivity{}, errors.New("活动不存在")
	}
	return a, nil

}

// 砍价商品查询
func GetCutGoodsDetail(goodsId int64) (models.CutGoods, error) {
	// 砍价商品查询
	var g models.CutGoods
	g.Id = uint32(goodsId)
	if err := g.GetById(); err != nil {
		return models.CutGoods{}, errors.New("查询商品失败" + err.Error())
	}
	return g, nil
}

// 用户参与活动查询
func GetUserJoin(joinId int64) (models.CutUserJoin, error) {
	var j models.CutUserJoin
	j.Id = uint32(joinId)
	if err := j.GetByJoinId(); err != nil {
		return models.CutUserJoin{}, errors.New("获取用户参加的记录失败" + err.Error())
	}
	return j, nil
}

// 帮忙砍价
func HelpCutPrice(in *__.HelpCutPriceReq) (float64, error) {
	createTime, toHelpUserId, goodsId, joinId, err := ParseCutInvitationCode(in.InvitationCode)
	if err != nil {
		return 0, errors.New("解析失败" + err.Error())
	}
	fmt.Println(toHelpUserId, goodsId, joinId)
	if time.Now().Unix()-createTime.Unix() > 60*10 {
		return 0, errors.New("邀请码已过期")
	}

	// 砍价商品查询
	g, err := GetCutGoodsDetail(int64(goodsId))
	if err != nil {
		return 0, err
	}
	// 活动查询
	a, err := GetCutActivity(int(g.ActivityId))
	if err != nil {
		return 0, err
	}
	if a.StopTime.Unix() < time.Now().Unix() {
		if a.Status != 3 {
			a.Status = 3
			err = a.UpdateActivity()
			if err != nil {
				return 0, errors.New("修改活动状态失败" + err.Error())
			}
		}
		return 0, errors.New("活动已过期")
	}

	// 用户参加的记录查询
	j, err := GetUserJoin(int64(joinId))
	if err != nil {
		return 0, err
	}
	if j.Id == 0 {
		return 0, errors.New("记录不存在")
	}
	if g.IsMyCut == 1 {
		if in.UserId == int64(toHelpUserId) {
			return 0, errors.New("不可以自己给自己砍价")
		}
	}

	// 获取用户的帮助记录
	var h models.CutUserHelp
	h.Uid = uint32(in.UserId)
	h.ToHelpId = int32(toHelpUserId)
	h.JoinId = uint32(joinId)
	count := h.GetHelpOneRecord()
	if count >= int64(g.CutNum) {
		return 0, errors.New("你已经帮他砍过了")
	}

	// 获取此次砍价的金额
	price, err := GetCutPrice(int64(toHelpUserId), int64(goodsId), int64(joinId), g, j)
	if err != nil {
		return 0, err
	}

	h = models.CutUserHelp{
		Uid:      uint32(in.UserId),
		ToHelpId: int32(toHelpUserId),
		JoinId:   uint32(joinId),
		Price:    price,
		CreateAt: time.Now(),
	}
	tx := config.DB.Begin()

	if err = h.CreateHelp(tx); err != nil {
		tx.Rollback()
		return 0, errors.New("帮砍失败" + err.Error())
	}

	j.IsCutPrice += price

	if err = j.UpdateJoin(tx); err != nil {
		tx.Rollback()
		return 0, errors.New("修改记录失败" + err.Error())
	}
	if j.CutPrice == j.CutPriceMin {
		j.Status = 3
		if err = j.UpdateJoin(tx); err != nil {
			tx.Rollback()
			return 0, errors.New("修改记录状态失败" + err.Error())
		}
	}
	if err = tx.Commit().Error; err != nil {
		tx.Rollback()
		return 0, errors.New("提交失败" + err.Error())
	}
	return price, nil
}

// 添加砍价活动，通过开始时间判断直接赋值status
func CreateCutActivity(in *__.CreateCutActivityReq) (uint32, error) {
	start, err := time.Parse(time.DateTime, in.StartTime)
	if err != nil {
		return 0, errors.New("开始时间格式错误")
	}
	end, err := time.Parse(time.DateTime, in.StopTime)
	if err != nil {
		return 0, errors.New("结束时间格式错误")
	}
	if start.Unix() > end.Unix() {
		return 0, errors.New("结束时间不得少于开始时间")
	}
	if end.Unix() < time.Now().Unix() {
		return 0, errors.New("结束时间不得在现有时间之前")
	}
	var status int8
	if time.Now().Unix() < start.Unix() {
		status = 1
	}
	if time.Now().Unix() > start.Unix() {
		status = 2
	}

	if in.JoinNum < 1 {
		return 0, errors.New("参加次数不得小于1")
	}
	c := models.CutActivity{
		Title:     in.Title,
		Image:     in.Image,
		StartTime: start,
		StopTime:  end,
		Info:      in.Info,
		Rule:      in.Rule,
		Status:    status,
		JoinNum:   uint32(in.JoinNum),
	}
	if err = c.CreateNewCutActivity(); err != nil {
		return 0, errors.New("添加活动失败" + err.Error())
	}
	return c.Id, nil
}

// 添加活动商品
func AddCutGoods(in *__.AddCutGoodsReq) (uint32, error) {
	var a models.CutActivity
	a.Id = uint32(in.ActivityId)
	if err := a.GetById(); err != nil {
		return 0, errors.New("查询活动失败" + err.Error())
	}
	if a.StopTime.Unix() < time.Now().Unix() {
		if a.Status != 3 {
			a.Status = 3
			err := a.UpdateActivity()
			if err != nil {
				return 0, errors.New("修改活动状态失败" + err.Error())
			}
		}
	}
	if a.Title == "" || a.Status == 3 {
		return 0, errors.New("活动不存在或活动已结束")
	}
	if in.MaxPerson < in.MinPerson {
		return 0, errors.New("最大人数不得小于最低人数")
	}
	if in.MinBuy > in.MaxBuy {
		return 0, errors.New("最大购买量不得小于最低购买量")
	}

	var goods models.Goods
	goods.Id = uint32(in.GoodsId)
	if err := goods.GoodsDetail(); err != nil {
		return 0, errors.New("获取仓库商品失败")
	}
	var g models.CutGoods
	g = models.CutGoods{
		ActivityId:  int32(in.ActivityId),
		GoodsName:   goods.GoodsName,
		GoodsImg:    goods.GoodsUrl,
		Stock:       int32(in.Stock),
		Desc:        goods.GoodsDesc,
		BasePrice:   goods.GoodsPrice,
		CutMaxPrice: float64(in.CutMaxPrice),
		CutMinPrice: float64(in.CutMinPrice),
		CutNum:      int32(in.CutNum),
		MaxBuy:      int32(in.MaxBuy),
		MinBuy:      int32(in.MinBuy),
		MinPrice:    float64(in.MinPrice),
		MaxPerson:   int32(in.MaxPerson),
		MinPerson:   int32(in.MinPerson),
	}
	if err := g.AddCutGoods(); err != nil {
		return 0, errors.New("添加活动商品失败" + err.Error())
	}
	return g.Id, nil
}

// 用户参加活动
func UserJoinActivity(in *__.UserJoinCutActivityReq) (uint32, error) {
	// 查询商品
	var g models.CutGoods
	g.Id = uint32(int32(in.GoodsId))
	if err := g.GetById(); err != nil {
		return 0, errors.New("查询商品失败" + err.Error())
	}
	if g.Stock == 0 {
		return 0, errors.New("库存不足")
	}

	// 查询活动
	a, err := GetCutActivity(int(g.ActivityId))
	if err != nil {
		return 0, err
	}
	if a.StopTime.Unix() < time.Now().Unix() {
		if a.Status != 3 {
			a.Status = 3
			err = a.UpdateActivity()
			if err != nil {
				return 0, errors.New("修改活动状态失败" + err.Error())
			}
		}
	}
	if a.Title == "" || a.Status == 3 {
		return 0, errors.New("活动不存在或活动已结束")
	}
	// 判断购买数量
	if in.Num > int64(g.MaxBuy) || in.Num < int64(g.MinBuy) {
		return 0, errors.New(fmt.Sprintf("购买数量不符合规定,最多购买%v个", g.MaxBuy))
	}
	var join models.CutUserJoin
	join.Uid = uint32(in.UserId)
	join.CutGoodsId = uint32(in.GoodsId)
	count := join.JoinOneActivityCount()
	if count >= int64(a.JoinNum) {
		return 0, errors.New("你已经达到最大参与次数")
	}

	tx := config.DB.Begin()

	//创建参与记录
	var j models.CutUserJoin
	j = models.CutUserJoin{
		Uid:         uint32(in.UserId),
		CutGoodsId:  uint32(in.GoodsId),
		CutPriceMin: g.MinPrice,
		CutPrice:    g.BasePrice * float64(in.Num),
		Num:         uint32(in.Num),
		Status:      1,
	}
	if err = j.CreateUserJoin(tx); err != nil {
		tx.Rollback()
		return 0, errors.New("参与活动失败" + err.Error())
	}

	g.Stock -= int32(in.Num)
	if err = g.UpdateCutGoods(); err != nil {
		tx.Rollback()
		return 0, errors.New("取库失败" + err.Error())
	}

	if err = tx.Commit().Error; err != nil {
		tx.Rollback()
		return 0, errors.New("取库失败" + err.Error())
	}

	return j.Id, nil
}

// 获取砍价商品列表
func CutGoodsList(activityId int) ([]*__.CutGoodsList, error) {
	var g models.CutGoods
	g.ActivityId = int32(activityId)
	goods, err := g.GoodsList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var goodList []*__.CutGoodsList
	for _, v := range goods {
		list := __.CutGoodsList{
			Id:          int64(v.Id),
			Title:       v.Title,
			GoodsName:   v.GoodsName,
			GoodsImg:    v.GoodsImg,
			Stock:       int64(v.Stock),
			Desc:        v.Desc,
			BasePrice:   float32(v.BasePrice),
			CutMaxPrice: float32(v.CutMaxPrice),
			CutMinPrice: float32(v.CutMinPrice),
			CutNum:      int64(v.CutNum),
			MinPrice:    float32(v.MinPrice),
			MinPerson:   int64(v.MinPerson),
			MaxPerson:   int64(v.MaxPerson),
			StartTime:   v.StartTime.Format(time.DateTime),
			StopTime:    v.StopTime.Format(time.DateTime),
			Image:       v.Image,
			MaxBuy:      int64(v.MaxBuy),
			MinBuy:      int64(v.MinBuy),
			ActivityId:  int64(v.ActivityId),
		}
		goodList = append(goodList, &list)
	}
	return goodList, err
}

// 根据砍价次数返回砍掉的价格
func GetCutPrice(toHelpUserId, goodsId, joinId int64, g models.CutGoods, j models.CutUserJoin) (float64, error) {

	// 检查是否已达到最低价
	remainingPrice := j.CutPrice - j.IsCutPrice
	if remainingPrice <= g.MinPrice {
		return 0, errors.New("已经达到最低价")
	}

	// 获取当前砍价次数
	currentCount := GetCutCount(joinId)

	// 检查是否超过最大砍价人数
	if currentCount >= int64(g.MaxPerson*int32(j.Num)/g.MinBuy) {
		return 0, errors.New("已达到最大砍价人数")
	}
	fmt.Println("最大人数", int64(g.MaxPerson*int32(j.Num)/g.MinBuy))
	// 第一刀判断 - 第一刀为总价的 50% —— 70%
	var price float64
	if currentCount == 0 {
		minPrice := j.CutPrice * 0.5
		maxPrice := j.CutPrice * 0.7
		price = minPrice + rand.Float64()*(maxPrice-minPrice)

		// 确保第一刀不会低于最低价
		if j.IsCutPrice+price >= j.CutPrice-g.MinPrice {
			price = j.CutPrice - j.IsCutPrice - g.MinPrice
		}
		return price, nil
	}

	// 最后一刀判断 -  当人数达到目标后砍下剩下的所有价格
	if currentCount+1 == int64(g.MaxPerson*int32(j.Num)/g.MinBuy) {
		// 剩下的价格
		price = j.CutPrice - j.IsCutPrice - g.MinPrice
		return price, nil
	}

	// 中间刀数判断
	price = GetMidCutPrice(goodsId, j, g)

	// 确保不超过剩余可砍金额
	if j.IsCutPrice+price > j.CutPrice-g.MinPrice {
		price = j.CutPrice - j.IsCutPrice - g.MinPrice
	}

	return price, nil
}

// 中间刀数价格
func GetMidCutPrice(joinId int64, j models.CutUserJoin, g models.CutGoods) float64 {
	// 查询砍价的人数
	count := GetCutCount(joinId)

	// 计算剩余可砍金额和剩余砍价次数
	remainingPrice := j.CutPrice - g.MinPrice - 0.01
	// 除去前后两刀可砍的价格
	CutPerson := g.MaxPerson - 2 - int32(count)
	// 平均每刀可砍的价格
	avg := remainingPrice / float64(CutPerson)

	var price float64

	// 根据当前进度调整砍价金额范围
	switch {
	// 前期：砍价金额稍高
	case float64(count)/float64(g.MaxPerson) < 0.3:
		minPrice := avg * 1.2
		maxPrice := avg * 1.5
		price = minPrice + rand.Float64()*(maxPrice-minPrice)
	// 中期：砍价金额适中
	case float64(count)/float64(g.MaxPerson) >= 0.3 && float64(count)/float64(g.MaxPerson) <= 0.7:
		minPrice := avg * 0.8
		maxPrice := avg * 1.1
		price = minPrice + rand.Float64()*(maxPrice-minPrice)
	// 后期：砍价金额较低
	case float64(count)/float64(g.MaxPerson) > 0.7:
		minPrice := avg * 0.5
		maxPrice := avg * 0.8
		price = minPrice + rand.Float64()*(maxPrice-minPrice)
	}

	// 确保不低于最小砍价金额，不高于最大砍价金额
	if price < g.CutMinPrice {
		price = g.CutMinPrice
	}
	if price > g.CutMaxPrice {
		price = g.CutMaxPrice
	}

	return price
}

// 获取砍价进度
func GetCutCount(joinId int64) int64 {
	var h models.CutUserHelp
	h.JoinId = uint32(joinId)
	count := h.GetHelpRecordCount()
	return count
}

// 砍价商品详情
func CutGoodsDetail(goodsId int) (models.CutGoodsList, error) {
	var g models.CutGoods
	g.Id = uint32(int32(goodsId))
	res, err := g.TwoTableDetailGoods()
	if err != nil {
		return models.CutGoodsList{}, errors.New("查询失败" + err.Error())
	}
	return res, nil
}

// 砍价活动列表
func CutActivityList(status int) ([]*__.CutActivityList, error) {
	var a models.CutActivity
	a.Status = int8(status)
	list, err := a.ActivityList()
	if err != nil {
		return nil, errors.New("活动列表查询失败" + err.Error())
	}
	var activityList []*__.CutActivityList
	for _, v := range list {
		l := __.CutActivityList{
			Id:        int64(v.Id),
			Title:     v.Title,
			Image:     v.Image,
			StartTime: v.StartTime.Format(time.DateTime),
			StopTime:  v.StopTime.Format(time.DateTime),
			Info:      v.Info,
			Rule:      v.Rule,
			Status:    int64(v.Status),
		}
		activityList = append(activityList, &l)
	}
	return activityList, nil
}

// 砍价活动详情
func CutActivityDetail(activityId int64) (models.CutActivity, error) {
	var a models.CutActivity
	a.Id = uint32(activityId)

	// 查询活动信息
	if err := a.GetById(); err != nil {
		return models.CutActivity{}, fmt.Errorf("查询活动信息失败: %v", err)
	}

	// 检查时间并更新状态（如果需要）
	if shouldUpdateStatus(a) {
		if err := updateActivityStatus(&a); err != nil {
			// 记录错误但不中断流程
			log.Printf("警告: 更新活动状态失败: %v", err)
		} else {
			// 状态更新成功，重新获取数据确保一致性
			if err := a.GetById(); err != nil {
				log.Printf("警告: 重新查询活动信息失败: %v", err)
			}
		}
	}

	return a, nil
}

// 判断是否需要更新状态
func shouldUpdateStatus(a models.CutActivity) bool {
	now := time.Now().Unix()
	startTime := a.StartTime.Unix()
	stopTime := a.StopTime.Unix()

	// 活动进行中
	if startTime < now && stopTime > now {
		return a.Status != 2
	}

	// 活动已结束
	if stopTime < now {
		return a.Status != 3
	}

	// 活动未开始
	return a.Status != 1 // 假设1是未开始状态
}

// 更新活动状态
func updateActivityStatus(a *models.CutActivity) error {
	now := time.Now().Unix()
	startTime := a.StartTime.Unix()
	stopTime := a.StopTime.Unix()

	if startTime < now && stopTime > now {
		a.Status = 2 // 进行中
	} else if stopTime < now {
		a.Status = 3 // 已结束
	} else {
		a.Status = 1 // 未开始
	}
	return a.UpdateActivity()
}

// 用户被帮助表列表
func UserHelpList(toHelpId int64) ([]*__.GetHelpList, error) {
	var h models.CutUserHelp
	h.ToHelpId = int32(toHelpId)
	list, err := h.ListByToUserId()
	if err != nil {
		return nil, errors.New("查询被帮助记录失败" + err.Error())
	}
	var helpList []*__.GetHelpList
	for _, v := range list {
		l := __.GetHelpList{
			Id:       int64(v.Id),
			Uid:      int64(v.Uid),
			ToHelpId: int64(v.ToHelpId),
			JoinId:   int64(v.JoinId),
			Price:    float32(v.Price),
			NickName: v.Nickname,
		}
		helpList = append(helpList, &l)
	}
	return helpList, nil
}

// 用户参加的记录列表
func GetUserJoinActivityList(in *__.GetUserJoinActivityListReq) ([]*__.UserJoinCutActivityList, error) {
	var j models.CutUserJoin
	j.Uid = uint32(in.Uid)
	list, err := j.UserJoinList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var joinList []*__.UserJoinCutActivityList
	for _, v := range list {
		l := __.UserJoinCutActivityList{
			Id:          int64(v.Id),
			Uid:         int64(v.Uid),
			CutGoodsId:  int64(v.CutGoodsId),
			CutPriceMin: float32(v.CutPriceMin),
			CutPrice:    float32(v.CutPrice),
			IsCutPrice:  float32(v.IsCutPrice),
			Status:      int64(v.Status),
			Num:         int64(v.Num),
		}
		joinList = append(joinList, &l)
	}
	return joinList, nil
}

// 用户帮助砍价列表
func GetUserToHelpList(in *__.GetUserToHelpListReq) ([]*__.GetHelpList, error) {
	var h models.CutUserHelp
	h.Uid = uint32(in.UId)
	list, err := h.UserToHelpList()
	if err != nil {
		return nil, errors.New("查询帮助记录失败" + err.Error())
	}
	var helpList []*__.GetHelpList
	for _, v := range list {
		l := __.GetHelpList{
			Id:       int64(v.Id),
			Uid:      int64(v.Uid),
			ToHelpId: int64(v.ToHelpId),
			JoinId:   int64(v.JoinId),
			Price:    float32(v.Price),
			NickName: v.Nickname,
		}
		helpList = append(helpList, &l)
	}
	return helpList, nil
}

// 修改活动
func UpdateCutActivity(in *__.UpdateCutActivityReq) error {
	start, err := time.Parse(time.DateTime, in.StartTime)
	if err != nil {
		return errors.New("开始时间格式错误")
	}
	end, err := time.Parse(time.DateTime, in.StopTime)
	if err != nil {
		return errors.New("结束时间格式错误")
	}
	if start.Unix() > end.Unix() {
		return errors.New("结束时间不得少于开始时间")
	}
	if end.Unix() < time.Now().Unix() {
		return errors.New("结束时间不得在现有时间之前")
	}
	var status int8
	if time.Now().Unix() < start.Unix() {
		status = 2
	}
	if time.Now().Unix() > start.Unix() {
		status = 1
	}
	var a models.CutActivity
	a.Id = uint32(in.Id)
	if err = a.GetById(); err != nil {
		return errors.New("查询失败" + err.Error())
	}
	a = models.CutActivity{
		Id:        uint32(in.Id),
		Title:     in.Title,
		Image:     in.Image,
		StartTime: start,
		StopTime:  end,
		Info:      in.Info,
		Rule:      in.Rule,
		Status:    status,
	}
	if err = a.UpdateActivity(); err != nil {
		return errors.New("修改失败" + err.Error())
	}
	return nil
}

// 修改商品详情
func UpdateCutGoods(in *__.UpdateCutGoodsReq) error {
	g, err := GetCutGoodsDetail(in.Id)
	if err != nil {
		return err
	}
	g = models.CutGoods{
		Id:          uint32(in.Id),
		ActivityId:  int32(in.ActivityId),
		GoodsId:     int32(in.GoodsId),
		GoodsName:   in.GoodsName,
		GoodsImg:    in.GoodsImg,
		Stock:       int32(in.Stock),
		Desc:        in.Desc,
		BasePrice:   float64(in.BasePrice),
		CutMaxPrice: float64(in.CutMaxPrice),
		CutMinPrice: float64(in.CutMinPrice),
		CutNum:      in.CutNum,
		IsMyCut:     int8(in.IsMyCut),
		MinPrice:    float64(in.MinPrice),
		MaxPerson:   in.MaxPerson,
		MinPerson:   in.MinPerson,
		MaxBuy:      in.MaxBuy,
		MinBuy:      in.MinBuy,
	}
	if err = g.UpdateCutGoods(); err != nil {
		return errors.New("修改商品失败" + err.Error())
	}
	return nil
}

// 砍价记录详情
func JoinDetail(in *__.JoinDetailReq) (models.CutUserJoin, error) {
	var j models.CutUserJoin
	j.Id = uint32(in.Id)
	if err := j.JoinDetail(); err != nil {
		return models.CutUserJoin{}, errors.New("查询记录失败" + err.Error())
	}
	fmt.Println(j)
	// 砍价商品查询
	g, err := GetCutGoodsDetail(int64(j.CutGoodsId))
	if err != nil {
		return models.CutUserJoin{}, err
	}
	var h models.CutUserHelp
	h.JoinId = j.Id
	count := h.GetHelpRecordCount()
	// 砍价人数已达到最低参与人数
	if count < int64(g.MinPerson) {
		if j.Status != 2 {
			j.Status = 2
			if err = j.UpdateJoinStatus(); err != nil {
				return models.CutUserJoin{}, errors.New("修改状态失败")
			}
		}
	}
	a, err := GetCutActivity(int(g.ActivityId))
	if err != nil {
		return models.CutUserJoin{}, err
	}
	if a.StopTime.Unix() < time.Now().Unix() {
		// 砍价人数未达到最低参与人数
		if count >= int64(g.MinPerson) {
			if j.Status != 3 {
				j.Status = 3

				if err = j.UpdateJoinStatus(); err != nil {
					return models.CutUserJoin{}, errors.New("修改状态失败")
				}
				g.Stock += int32(j.Num)
				err = g.UpdateCutGoods()
				if err != nil {
					fmt.Errorf(fmt.Sprintf("活动记录id为的%v返回库存失败"+err.Error()), j.Id)
				}
			}
		}
	}
	// 砍价人数未达到最低参与人数
	if count >= int64(g.MinPerson) {
		if j.Status != 3 {
			j.Status = 3

			if err = j.UpdateJoinStatus(); err != nil {
				return models.CutUserJoin{}, errors.New("修改状态失败")
			}
			g.Stock += int32(j.Num)
			err = g.UpdateCutGoods()
			if err != nil {
				fmt.Errorf(fmt.Sprintf("活动记录id为的%v返回库存失败"+err.Error()), j.Id)
			}
		}
	}
	return j, nil
}

// 获取进度
func JoinProgress(j models.CutUserJoin) (float64, error) {
	var g models.CutGoods
	g.Id = j.CutGoodsId
	if err := g.GoodsDetail(); err != nil {
		return 0, errors.New("查询商品失败" + err.Error())
	}
	var h models.CutUserHelp
	h.JoinId = j.Id
	count := h.GetHelpRecordCount()
	if count == 0 {
		return 0, nil
	}
	progress := float64(count) / float64(g.MaxPerson)
	return progress, nil
}

// 记录列表
func JoinList() ([]*__.CutJoinList, error) {
	var j models.CutUserJoin
	list, err := j.CutJoinList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var joinList []*__.CutJoinList
	for _, v := range list {
		l := __.CutJoinList{
			Id:          int64(v.Id),
			Uid:         int64(v.Uid),
			CutGoodsId:  int64(v.CutGoodsId),
			CutPriceMin: float32(v.CutPriceMin),
			CutPrice:    float32(v.IsCutPrice),
			IsCutPrice:  float32(v.IsCutPrice),
			Status:      int64(v.Status),
			Num:         int64(v.Num),
		}
		joinList = append(joinList, &l)
	}
	return joinList, nil
}

// 帮助列表
func HelpList() ([]*__.GetHelpList, error) {
	var h models.CutUserHelp
	list, err := h.HelpList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var helpList []*__.GetHelpList
	for _, v := range list {
		l := __.GetHelpList{
			Id:       int64(v.Id),
			Uid:      int64(v.Uid),
			ToHelpId: int64(v.ToHelpId),
			JoinId:   int64(v.JoinId),
			Price:    float32(v.Price),
			NickName: v.Nickname,
		}
		helpList = append(helpList, &l)
	}
	return helpList, err
}
